#!/usr/bin/env/python3
# -*- coding: utf-8 -*-

"""
@Time    : 2020/3/31 11:03
@Author  : Chen Liu
@FileName: remove_freque.py
@Software: PyCharm
"""


# 删除排序数组中的重复元素
# 解法一：思路错误:只想着把重复的元素放到前面，但是没有保留后面重复的元素应该放的位置
# class Solution:
#     def removeDuplicates(self, nums) -> int:
#         if not nums:
#             return
#         last = nums[0]
#         count = 1
#         for i in range(1, len(nums) - 1):
#             print("i: ", i)
#             print("last: ", last)
#             j = i
#             while j < len(nums) and last == nums[j]:
# #                 j += 1
#             if j >= len(nums):
#                 break
#             print("%d, %d" % (i, j))
#             nums[i] = nums[j]
#             count += 1
#             last = nums[j]
#
#             print("nums: ", nums)
#
#         return count


# 解法二：双指针，指针s从头开始更新每个元素，f指针遍历原来的数组，并判断是否存在重复元素。
class Solution:
    def removeDuplicates(self, nums):
        if len(nums) <= 1:
            return len(nums)

        s = 0
        for f in range(1, len(nums)):
            if nums[s] != nums[f]:
                s += 1
                nums[s] = nums[f]

        return s + 1


if __name__ == "__main__":
    s = Solution()
    nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
    count = s.removeDuplicates(nums)
    print(count)
    print(nums[:count])
